package com.neusoft.demo3.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.neusoft.demo3.bean.SysUser;
import com.neusoft.demo3.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class SysUserController {
    @Autowired
    private SysUserMapper sysUserMapper;
    @GetMapping("userlist")
    public  Object finduser(){

        return sysUserMapper.selectList(null);
    }

    @PostMapping("login")
    public Object login(@RequestBody SysUser user) {
        // 1. 判断请求参数是否为空
        if (user == null) {
            return "错误：请求参数不能为空";
        }
        if (StringUtils.isEmpty(user.getUser_name())) { // 需导入 org.springframework.util.StringUtils
            return "错误：用户名不能为空";
        }
        if (StringUtils.isEmpty(user.getUser_pwd())) {
            return "错误：密码不能为空";
        }

        // 2. 调用 Mapper 查询用户（实际应通过 Service 层）
        SysUser loginUser = sysUserMapper.login(user.getUser_name(), user.getUser_pwd());

        // 3. 根据查询结果判断并返回响应
        if (loginUser != null) {
            // 登录成功：隐藏密码，返回用户信息
            loginUser.setUser_pwd(null);
            return loginUser;
        } else {
            // 登录失败：返回错误提示
            return "错误：用户名或密码不正确";
        }
    }
}
